<template>
    <div>
        <el-dialog
                :title="quoteParams.title"
                :visible.sync="quoteParams.visible"
                :close-on-click-modal="false"
                width="1000px">
            <div>
                <el-form :inline="true" :model="formData" class="demo-form-inline">
                    <el-form-item label="层面/角度">
                        <el-cascader
                                @change="changeRange"
                                v-model="formData.rectificationRangeList"
                                :options="rangeList"
                                filterable
                                :props="{label: 'name',value: 'id',children: 'rectificationRangeDTOList'}">
                        </el-cascader>
                    </el-form-item>
                    <el-form-item label="年份">
                        <el-select v-model="formData.year" placeholder="请选择年份">
                            <el-option
                                    v-for="year in yearArr"
                                    :key="year"
                                    :label="`${year}年`"
                                    :value="year"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="目标标准">
                        <el-select v-model="formData.targetStandardId" placeholder="请选择目标标准">
                            <el-option v-for="(item,index) in targetStandardList" :key="index" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="getPointList">查询</el-button>
                    </el-form-item>
                </el-form>

                <myTable
                        :tableType="'multipleTable'"
                        :tableData="tableData"
                        :column="column"
                        :selection=true
                        :selectionType="'quotePoint'"
                        @selectionChange="selectionChange"></myTable>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="quoteParams.visible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm">确定引用</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {getStorage} from "@/config/utils";
    import myTable from '@/components/table/index'
    import { getModelPointList, getTargetStandardListAll, addModelPointData} from "../../../../api/target";

    export default {
        name: 'quotePoint',
        components: {
            myTable
        },
        props: {
            quoteParams: {
                type: Object
            },
            pointForm: {
                type: Object
            }
        },
        computed: {
            yearArr () {
                return this.$config.yearArr()
            },
            indexOneArr() {
                let systemInfo = getStorage('systemInfo');
                if (systemInfo && systemInfo.length) {
                    let temp = systemInfo.filter(item => item.code === this.$config.xtWZ);
                    if (temp && temp.length) {
                        return temp[0].dictValDTOList
                    }
                }
                return []
            }
        },
        data () {
            return {
                formData: {
                    year: '',
                    rectificationRangeId: '',
                    rectificationRangeList: [],
                    targetStandardId: ''
                },
                column: [
                    {
                        prop: 'indexOne',
                        label: '一级指标'
                    },
                    {
                        prop: 'indexTwo',
                        label: '二级指标'
                    },
                    {
                        prop: 'indexThree',
                        label: '三级指标'
                    },
                    {
                        prop: 'name',
                        label: '质控点'
                    }
                ],
                tableData: [],
                targetStandardId: '',
                rangeList: [],
                targetStandardList: [],
                pointInfo: null //选择的质控点信息
            }
        },
        methods: {
            // 查询质控点列表
            getPointList () {
                if (!this.formData.rectificationRangeId) {
                    this.$message.warning('请选择层面/角度查询目标标准!');
                    return
                }
                if (!this.formData.targetStandardId) {
                    this.$message.warning('请选择目标标准查询质控点!');
                    return
                }
                getModelPointList({targetStandardId: this.formData.targetStandardId}).then(res => {
                    this.tableData = res || []
                }).catch(() => {})
            },
            // 查询下拉目标标准
            getTargetList () {
                let params = {
                    rectificationRangeId:this.formData.rectificationRangeId,
                    year: this.formData.year
                }
                getTargetStandardListAll(params).then(res => {
                    this.targetStandardList = res || []
                })
            },
            // 选择引用的质控点
            selectionChange (val) {
                // 单选
                let len = val.length
                if (len > 1) {
                    this.$nextTick(() => {
                        this.$refs.multipleTable.$refs.table.clearSelection()
                        this.$refs.multipleTable.$refs.table.toggleRowSelection(val.pop())
                    });
                } else {
                    this.pointInfo = len ? val[0] : null
                }
            },
            // 添加引用的质控点
            submitForm () {
                if (!this.pointInfo) {
                    this.$message.warning('请选择一条质控点！');
                } else {
                    this.pointInfo.targetStandardId = +this.quoteParams.targetStandardId
                    addModelPointData(this.pointInfo).then(res => {
                        this.$message.success('引用成功！');
                        this.$emit('getList')
                        this.quoteParams.visible = false
                    }).catch(() => {})
                }
            },
            // 选择层面角度
            changeRange (val) {
                this.formData.rectificationRangeId = val[1]
                this.getTargetList()
            }
        },
        created () {
            // this.formData.targetStandardId = +this.quoteParams.targetStandardId
            // this.formData.rectificationRangeId = +this.quoteParams.rectificationRangeId
            this.formData.year = (new Date()).getFullYear()

            // this.rangeList = JSON.parse(sessionStorage.getItem('rangeList')) || []
            let temp = getStorage('rangeList');
            temp.forEach(item => {
                item.id = 0;
                return item.rectificationRangeDTOList
            });
            this.rangeList = temp

            // this.getPointList()
            // this.getTargetList()
        }
    }
</script>

<style scoped>

</style>